package wxdb.ui;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.util.ArrayList;
import java.util.Iterator;

import javax.swing.*;

public class ChangeRights extends JDialog{
	private String[] databases = new String[Global.UI_REGISTER_DBMAX];
	private JList databaseList;
	
	private JButton chButton = new JButton("Change");
	private JButton clButton = new JButton("Cancel");
	
	private JLabel crLabel = new JLabel("Change Rights :");
	
	private JComboBox cmb = new JComboBox();   
	
	private JPanel pane = new JPanel();
	
	private void setMyPane(){
		GridBagLayout gridbag = new GridBagLayout();
		GridBagConstraints constraints = new GridBagConstraints();
		pane.setLayout(gridbag);

		constraints=new GridBagConstraints();
		constraints.insets=new Insets(5,5,5,5);
		constraints.gridwidth=GridBagConstraints.REMAINDER;
		gridbag.setConstraints(crLabel, constraints);
		pane.add(crLabel);
		
		constraints=new GridBagConstraints();
		constraints.gridwidth=GridBagConstraints.REMAINDER;
		constraints.insets=new Insets(5,5,5,5);
		gridbag.setConstraints(cmb, constraints);
		pane.add(cmb);
		
		constraints=new GridBagConstraints();
		constraints.insets=new Insets(5,5,5,5);
		constraints.fill=GridBagConstraints.HORIZONTAL;
		constraints.gridwidth=GridBagConstraints.REMAINDER;
		gridbag.setConstraints(databaseList, constraints);
		pane.add(databaseList);

		constraints=new GridBagConstraints();
		constraints.insets=new Insets(5,5,5,5);
		constraints.anchor=GridBagConstraints.CENTER;
		gridbag.setConstraints(chButton, constraints);
		pane.add(chButton);
		
		constraints=new GridBagConstraints();
		constraints.insets=new Insets(5,5,5,5);
		constraints.gridwidth=GridBagConstraints.REMAINDER;
		gridbag.setConstraints(clButton, constraints);
		pane.add(clButton);
		
		setLocation(350,100);
		setContentPane(pane);
	}
	
	private void initListener(){
		ActListener actListener = new ActListener();
		chButton.addActionListener(actListener);
		clButton.addActionListener(actListener);
	}
	
	class ActListener implements ActionListener{
		public void actionPerformed(ActionEvent e){
			if(e.getSource() == chButton)
				chButtonRun();
			if(e.getSource() == clButton)
				dispose();
		}
	}
	
	private void chButtonRun(){
		String name = (String)cmb.getSelectedItem();
		
		String dbs = "";
		if(databaseList.getSelectedValues().length==0){
			JOptionPane.showMessageDialog(null,"Please select databases","Error",JOptionPane.ERROR_MESSAGE);
			return;
		}
		for(int i=0; i<databaseList.getSelectedValues().length ;i++){
			dbs += (String) databaseList.getSelectedValues()[i];
			dbs += ";";
		}
		Global.changeRights(name, dbs);
		this.dispose();
	}
	
	public ChangeRights(){
		File file = new File("config/databases.txt");
		BufferedReader reader = null;
		int i=0;
		try{
			reader = new BufferedReader(new FileReader(file));
			String dbName = null;
			while((dbName = reader.readLine())!=null){
				databases[i] = new String(dbName);
				i++;
			}
		}catch(Exception e){
			System.out.println(e.getMessage());
		}
		
		databaseList = new JList(databases);
		
		ArrayList<String> al = Global.getUsers();
		for (Iterator<String> iter = al.iterator(); iter.hasNext();) {
			String name = iter.next(); 
			if(name.equals("Administrator"))
				continue;
			cmb.addItem(name);
		}
		
		setTitle("Administrator-AddUser");
		setSize(350,250);
		initListener();
		setMyPane();
		setVisible(true);
		setResizable(false);		
	}
}
